using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMLookupListSimpleMustHaveItem
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region TestDMLookupListSimpleMustHaveItem

        [Test]
        public void Test_CreateTestDMLookupListSimpleMustHaveItem()
        {
            //---------------Set up test pack-------------------
            DMLookupListSimple dmLookupListSimple = TestUtilsShared.CreateUnsavedValidSimpleLookupListInfo();
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            dmProperty.LookupList = dmLookupListSimple;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmLookupListSimple.DMProperty);
            Assert.IsNotNull(dmProperty.Class);
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMLookupListSimpleMustHaveItemRule();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMLookupListSimpleMustHaveItem", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
        }

        [Test]
        public void Test_CreateTestDMLookupListSimpleMustHaveItem_NoClass()
        {
            //---------------Set up test pack-------------------
            DMLookupListSimple dmLookupListSimple = TestUtilsShared.CreateUnsavedValidSimpleLookupListInfo();
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            dmProperty.Class = null;
            dmProperty.LookupList = dmLookupListSimple;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmLookupListSimple.DMProperty);
            Assert.IsNull(dmProperty.Class);
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMLookupListSimpleMustHaveItemRule();
            rule.IsValid(dmLookupListSimple);
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMLookupListSimpleMustHaveItem", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
            StringAssert.Contains("The Lookup List for the Property '", rule.Message);
            StringAssert.Contains("'" + dmProperty + "'", rule.Message);
            StringAssert.Contains("' with no related Class must have at least one item.", rule.Message);
        }

        [Test]
        public void Test_CreateTestDMLookupListSimpleMustHaveItem_NoProperty()
        {
            //---------------Set up test pack-------------------
            DMLookupListSimple dmLookupListSimple = TestUtilsShared.CreateUnsavedValidSimpleLookupListInfo();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmLookupListSimple.DMProperty);
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMLookupListSimpleMustHaveItemRule();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMLookupListSimpleMustHaveItem", rule.Name);
//            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
//            StringAssert.Contains("The Lookup List which has no related Property ", rule.Message);
//            StringAssert.Contains("must have at least one item.", rule.Message);
        }

//        [Test]
//        public void Test_CreateTestDMLookupListSimpleMustHaveItem_WhenDMLookupListSimpleNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMLookupListSimpleMustHaveItemRule(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//            //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmLookupListSimple", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_TestDMLookupListSimpleMustHaveItem_FailsWhenLookupListSimpleHasNoItems()
        {
            //---------------Set up test pack-------------------
            DMLookupListSimple dmLookupListSimple = new DMLookupListSimple();
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            dmProperty.LookupList = dmLookupListSimple;
            IBusinessObjectRule rule = new DMLookupListSimpleMustHaveItemRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmLookupListSimple.ListItems.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupListSimple);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_TestDMLookupListSimpleMustHaveItem_PassesWhenLookupListSimpleHasItem()
        {
            //---------------Set up test pack-------------------
            DMLookupListSimple dmLookupListSimple = TestUtilsShared.CreateUnsavedValidSimpleLookupListInfo();
            dmLookupListSimple.ListItems.Add(new DMListItem());
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            dmProperty.LookupList = dmLookupListSimple;
            IBusinessObjectRule rule = new DMLookupListSimpleMustHaveItemRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dmLookupListSimple.ListItems.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupListSimple);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

    #endregion

    }

}